/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*
*	Julio Montes e-mail: imc.coder@gmail.com
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "include/iaMainFrame.h"

#include "include/iaManager.h"
#include "include/iaAboutDialog.h"
#include "sdk/include/editormgr.h"
#include "include/iaDialogManager.h"
#include "sdk/include/editor.h"
#include "sdk/include/cfgmgr.h"
#include "sdk/include/path.h"
#include "sdk/include/filecfg.h"
#include "sdk/include/plugin.h"
#include "sdk/include/pluginmgr.h"
#include "sdk/include/pluginsmgrdlg.h"
#include "sdk/include/mgr.h"
#include "sdk/include/notebookeditor.h"
#include "sdk/include/mainframe.h"
#include "include/iaFindDialog.h"
#include "include/iaReplaceDialog.h"
#include "include/iaSettingsEditorDialog.h"

BEGIN_EVENT_TABLE( iaMainFrame, wxFrame )
    EVT_CLOSE( iaMainFrame::OnClose )

    EVT_SCI_UPDATEUI( wxID_ANY, iaMainFrame::OnUpdateStyledEditor )
    EVT_AUINOTEBOOK_PAGE_CHANGED( sdk::EditorManager::ID_NOTEBOOKEDITOR, iaMainFrame::OnPageChanged )

///FILE MENU
    EVT_MENU( iaID_FILE_NEW_EMPTY_FILE,  iaMainFrame::OnNewEmptyFile )
    EVT_MENU( iaID_FILE_OPEN_FILE,  iaMainFrame::OnOpenFile )
    EVT_MENU( iaID_FILE_SAVE_FILE,  iaMainFrame::OnSaveFile )
    EVT_MENU( iaID_FILE_SAVE_FILE_AS,  iaMainFrame::OnSaveFileAs )
    EVT_MENU( iaID_FILE_SAVE_ALL_FILES,  iaMainFrame::OnSaveAllFiles )
    EVT_MENU( iaID_FILE_CLOSE_FILE,  iaMainFrame::OnCloseFile )
    EVT_MENU( iaID_FILE_CLOSE_ALL_FILES,  iaMainFrame::OnCloseAllFiles )
    EVT_MENU( iaID_FILE_EXIT,  iaMainFrame::OnExit )

    EVT_UPDATE_UI( iaID_FILE_NEW_EMPTY_FILE, iaMainFrame::OnUpdateFileMenu )

///EDIT MENU
    EVT_MENU_RANGE( iaID_EDIT_UNDO, iaID_EDIT_AUTOCOMPLETE, iaMainFrame::OnEditMenu )
    EVT_UPDATE_UI( iaID_EDIT_UNDO, iaMainFrame::OnUpdateEditMenu )

///VIEW MENU
    EVT_MENU_RANGE( iaID_VIEW_SUB_TOOLBAR, iaID_VIEW_STATUS_BAR, iaMainFrame::OnViewMenu )
    EVT_UPDATE_UI( iaID_VIEW_TOOLBAR_MAIN, iaMainFrame::OnUpdateViewMenu )

///SEARCH MENU
    EVT_MENU( iaID_SEARCH_FIND, iaMainFrame::OnFind )
    EVT_MENU( iaID_SEARCH_FIND_NEXT, iaMainFrame::OnFindNext )
    EVT_MENU( iaID_SEARCH_FIND_PREV, iaMainFrame::OnFindPrevious )
    EVT_MENU( iaID_SEARCH_REPLACE, iaMainFrame::OnReplace )
    EVT_MENU( iaID_SEARCH_GOTO_LINE, iaMainFrame::OnGotoLine )
    EVT_UPDATE_UI( iaID_SEARCH_FIND, iaMainFrame::OnUpdateSearchMenu )

///SETTINGS MENU
    EVT_MENU( iaID_SETTINGS_EDITOR, iaMainFrame::OnSettingsEditor )
    EVT_MENU( iaID_SETTINGS_PLUGINS, iaMainFrame::OnSettingsPlugins )


///HELP MENU
    EVT_MENU( iaID_HELP_ABOUT, iaMainFrame::OnHelpAbout )
END_EVENT_TABLE()


iaMainFrame::iaMainFrame()
    : sdk::MainFrame( NULL, iaID_MAIN_FRAME, wxT( "IDEALL" ) )
{
    SetIcon( wxIcon( sdk::Path::s_dirImages + wxT( "icon_window.png" ), wxBITMAP_TYPE_PNG ) );

    sdk::Manager::SetMainFrame( this );

    auiManager = new wxAuiManager( this );

    iaManager::Get()->CreateDialogManager( this );

    m_editorManager = sdk::EditorManager::Get();
    auiManager->AddPane( m_editorManager->GetNotebookEditor(), wxAuiPaneInfo().CentrePane() );

    iaManager::Get()->CreateProjectNotebook( this );
    projectManager = iaManager::Get()->GetProjectNotebook();
    auiManager->AddPane( projectManager, wxAuiPaneInfo().Left().Layer( 1 ).Caption( wxT( "Projects" ) ).BestSize( 200, 400 ) );

    iaManager::Get()->CreateLogNotebook( this );
    logManager = iaManager::Get()->GetLogNotebook();
    auiManager->AddPane( logManager, wxAuiPaneInfo().Bottom().Caption( wxT( "Log and Others" ) ).BestSize( 600, 200 ).MaximizeButton() );

    configManager = sdk::ConfigManager::Get();
    sdk::FileConfig *fcLayout = configManager->GetFile( sdk::Path::s_dirConfig + wxT( "settings_layout.xml" ) );

    auiManager->LoadPaneInfo( fcLayout->ReadString( wxT( "/perspective/manager/editor" ), wxT( "" ) ),
        auiManager->GetPane( m_editorManager->GetNotebookEditor() ) );

    auiManager->LoadPaneInfo( fcLayout->ReadString( wxT( "/perspective/manager/log" ), wxT( "" ) ),
        auiManager->GetPane( logManager ) );

    auiManager->LoadPaneInfo( fcLayout->ReadString( wxT( "/perspective/manager/project" ), wxT( "" ) ),
        auiManager->GetPane( projectManager ) );


///TOOLBAR
    mainToolBar = new wxToolBar( this, wxID_ANY );
    mainToolBar->AddTool( iaID_FILE_NEW_EMPTY_FILE, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "new_file.png" ), wxBITMAP_TYPE_PNG ), wxT( "New file" ) );
    mainToolBar->AddTool( iaID_FILE_OPEN_FILE, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "open_file.png" ), wxBITMAP_TYPE_PNG ), wxT( "Open file" ) );
    mainToolBar->AddTool( iaID_FILE_SAVE_FILE, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "save_file.png" ), wxBITMAP_TYPE_PNG ), wxT( "Save file" ) );
    mainToolBar->AddTool( iaID_FILE_SAVE_ALL_FILES, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "save_all_files.png" ), wxBITMAP_TYPE_PNG ), wxT( "Save all files" ) );
    mainToolBar->AddSeparator();
    mainToolBar->AddTool( iaID_EDIT_UNDO, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "undo.png" ), wxBITMAP_TYPE_PNG ), wxT( "Undo" ) );
    mainToolBar->AddTool( iaID_EDIT_REDO, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "redo.png" ), wxBITMAP_TYPE_PNG ), wxT( "Redo" ) );
    mainToolBar->AddSeparator();
    mainToolBar->AddTool( iaID_EDIT_CUT, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "cut.png" ), wxBITMAP_TYPE_PNG ), wxT( "Cut" ) );
    mainToolBar->AddTool( iaID_EDIT_COPY, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "copy.png" ), wxBITMAP_TYPE_PNG ), wxT( "Copy" ) );
    mainToolBar->AddTool( iaID_EDIT_PASTE, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "paste.png" ), wxBITMAP_TYPE_PNG ), wxT( "Paste" ) );
    mainToolBar->AddSeparator();
    mainToolBar->AddTool( iaID_SEARCH_FIND, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "find.png" ), wxBITMAP_TYPE_PNG ), wxT( "Find" ) );
    mainToolBar->AddTool( iaID_SEARCH_REPLACE, wxT( "" ), wxBitmap( sdk::Path::s_dirImages + wxT( "replace.png" ), wxBITMAP_TYPE_PNG ), wxT( "Replace" ) );

    auiManager->InsertPane( mainToolBar, wxAuiPaneInfo().ToolbarPane().Top().Floatable( false ) );

    auiManager->LoadPaneInfo( fcLayout->ReadString( wxT( "/perspective/toolbar/main" ), wxT( "" ) ),
        auiManager->GetPane( mainToolBar ) );

    const wxString &perspective( fcLayout->ReadString( wxT( "/perspective/manager" ), wxT( "" ) ) );
    if( perspective != wxEmptyString )
        auiManager->LoadPerspective( perspective );
    else
        auiManager->Update();



///STATUS BAR
    sbar = CreateStatusBar( 6 );
    int wPath[] = { 450, 100, 150, 100, 100, -1 };
    sbar->SetFieldsCount( 6, wPath );
    sbar->SetStatusText( wxT( "Welcome to IDEALL" ), 0 );



///MENU BAR
    wxMenuBar *menuBar = new wxMenuBar();

    /// FILE MENU
    wxMenu *fileMenu = new wxMenu;

    wxMenuItem *itemNewFile = new wxMenuItem( fileMenu, iaID_FILE_NEW_EMPTY_FILE, wxT( "New empty file\tCtrl+N" ) );
    itemNewFile->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "new_file.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemOpenFile = new wxMenuItem( fileMenu, iaID_FILE_OPEN_FILE, wxT( "Open file\tCtrl+O" ) );
    itemOpenFile->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "open_file.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemSaveFile = new wxMenuItem( fileMenu, iaID_FILE_SAVE_FILE, wxT( "Save file\tCtrl+S" ) );
    itemSaveFile->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "save_file.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemSaveFileAs = new wxMenuItem( fileMenu, iaID_FILE_SAVE_FILE_AS, wxT( "Save file as..." ) );
    itemSaveFileAs->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "save_file_as.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemSaveAllFiles = new wxMenuItem( fileMenu, iaID_FILE_SAVE_ALL_FILES, wxT( "Save all file\tShift+Ctrl+S" ) );
    itemSaveAllFiles->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "save_all_files.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemCloseFile = new wxMenuItem( fileMenu, iaID_FILE_CLOSE_FILE, wxT( "Close file\tCtrl+W" ) );
    itemCloseFile->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "close_file.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemCloseAllFiles = new wxMenuItem( fileMenu, iaID_FILE_CLOSE_ALL_FILES, wxT( "Close all files\tCtrl+Shift+W" ) );

    wxMenuItem *itemExit = new wxMenuItem( fileMenu, iaID_FILE_EXIT, wxT( "Exit\tCtrl+Q" ) );
    itemExit->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "exit.png" ), wxBITMAP_TYPE_PNG ) );

    fileMenu->Append( itemNewFile );
    fileMenu->AppendSeparator();
    fileMenu->Append( itemOpenFile );
    fileMenu->AppendSeparator();
    fileMenu->Append( itemSaveFile );
    fileMenu->Append( itemSaveFileAs );
    fileMenu->Append( itemSaveAllFiles );
    fileMenu->AppendSeparator();
    fileMenu->Append( itemCloseFile );
    fileMenu->Append( itemCloseAllFiles );
    fileMenu->AppendSeparator();
    fileMenu->Append( itemExit );

    menuBar->Append( fileMenu,      wxT( "&File" ) );
    /////////////////////////////


    /// EDIT MENU
    wxMenu *editMenu = new wxMenu;

    wxMenuItem *itemUndo = new wxMenuItem( editMenu, iaID_EDIT_UNDO, wxT( "Undo\tCtrl+Z" ) );
    itemUndo->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "undo.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemRedo = new wxMenuItem( editMenu, iaID_EDIT_REDO, wxT( "Redo\tCtrl+Y" ) );
    itemRedo->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "redo.png" ), wxBITMAP_TYPE_PNG ) );

//    wxMenuItem *itemClearHistory = new wxMenuItem( editMenu, iaID_EDIT_CLEAR_HISTORY, wxT( "Clear history" ) );
//    itemClearHistory->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "clear_history.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemCut = new wxMenuItem( editMenu, iaID_EDIT_CUT, wxT( "Cut\tCtrl+X" ) );
    itemCut->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "cut.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemCopy = new wxMenuItem( editMenu, iaID_EDIT_COPY, wxT( "Copy\tCtrl+C" ) );
    itemCopy->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "copy.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemPaste = new wxMenuItem( editMenu, iaID_EDIT_PASTE, wxT( "Paste\tCtrl+V" ) );
    itemPaste->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "paste.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemSelectAll = new wxMenuItem( editMenu, iaID_EDIT_SELECT_ALL, wxT( "Select all\tCtrl+A" ) );
    itemSelectAll->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "select_all.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemAutoComp = new wxMenuItem( editMenu, iaID_EDIT_AUTOCOMPLETE, wxT( "Auto complete\tCtrl+Space" ) );
    itemAutoComp->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "auto_complete.png" ), wxBITMAP_TYPE_PNG ) );

    editMenu->Append( itemUndo );
    editMenu->Append( itemRedo );
//    editMenu->Append( itemClearHistory );
    editMenu->AppendSeparator();
    editMenu->Append( itemCut );
    editMenu->Append( itemCopy );
    editMenu->Append( itemPaste );
    editMenu->AppendSeparator();
    editMenu->Append( itemSelectAll );
    editMenu->AppendSeparator();
    editMenu->Append( itemAutoComp );

    menuBar->Append( editMenu, wxT( "&Edit" ) );
    ////////////////////////////////////

    ///VIEW MENU
    wxMenu *viewMenu = new wxMenu;

    wxMenu *toolbarSubMenu = new wxMenu;
    toolbarSubMenu->AppendCheckItem( iaID_VIEW_TOOLBAR_MAIN, wxT( "Main" ) );

    viewMenu->AppendSubMenu( toolbarSubMenu, wxT( "Toolbars" ) );

    viewMenu->AppendCheckItem( iaID_VIEW_PROJECT, wxT( "Projects" ) );
    viewMenu->AppendCheckItem( iaID_VIEW_LOG, wxT( "Log and Others" ) );
    viewMenu->AppendCheckItem( iaID_VIEW_STATUS_BAR, wxT( "Status bar" ) );

    menuBar->Append( viewMenu, wxT( "&View" ) );
    /////////////////////////


    ///SEARCH MENU
    wxMenu *searchMenu = new wxMenu;

    wxMenuItem *itemFind = new wxMenuItem( searchMenu, iaID_SEARCH_FIND, wxT( "Find...\tCtrl+F" ) );
    itemFind->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "find.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemFindNext = new wxMenuItem( searchMenu, iaID_SEARCH_FIND_NEXT, wxT( "Find next\tF3" ) );
    itemFindNext->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "find_next.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemFindPrev = new wxMenuItem( searchMenu, iaID_SEARCH_FIND_PREV, wxT( "Find previous\tCtrl+F3" ) );
    itemFindPrev->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "find_previous.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemReplace = new wxMenuItem( searchMenu, iaID_SEARCH_REPLACE, wxT( "Replace...\tCtrl+R" ) );
    itemReplace->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "replace.png" ), wxBITMAP_TYPE_PNG ) );

    wxMenuItem *itemGotoLine = new wxMenuItem( searchMenu, iaID_SEARCH_GOTO_LINE, wxT( "Goto line\tCtrl+J" ) );
    itemGotoLine->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "goto_line.png" ), wxBITMAP_TYPE_PNG ) );


    searchMenu->Append( itemFind );
    searchMenu->Append( itemFindNext );
    searchMenu->Append( itemFindPrev );
    searchMenu->AppendSeparator();
    searchMenu->Append( itemReplace );
    searchMenu->AppendSeparator();
    searchMenu->Append( itemGotoLine );

    menuBar->Append( searchMenu, wxT( "Sea&rch" ) );
    /////////////////////////


    ///PLUGINS MENU
    //wxMenu *pluginsMenu = new wxMenu;



    //menuBar->Append( pluginsMenu, wxT( "P&lugins" ) );
    //////////////////////


    ///SETTINGS MENU
    wxMenu *settingsMenu = new wxMenu;
    wxMenuItem *itemEditor = new wxMenuItem( settingsMenu, iaID_SETTINGS_EDITOR, wxT( "Editor..." ) );
    wxMenuItem *itemPlugins = new wxMenuItem( settingsMenu, iaID_SETTINGS_PLUGINS, wxT( "Plugins..." ) );

    settingsMenu->Append( itemEditor );
    settingsMenu->AppendSeparator();
    settingsMenu->Append( itemPlugins );

    menuBar->Append( settingsMenu, wxT( "&Settings" ) );
    /////////////////////////


    ///HELP MENU
    wxMenu *helpMenu = new wxMenu;
    wxMenuItem *itemAbout = new wxMenuItem( helpMenu, iaID_HELP_ABOUT, wxT( "About..." ) );
    itemAbout->SetBitmap( wxBitmap( sdk::Path::s_dirImages + wxT( "About.png" ), wxBITMAP_TYPE_PNG ) );

    helpMenu->Append( itemAbout );

    menuBar->Append( helpMenu, wxT( "&Help" ) );
    /////////////////////////

    SetMenuBar( menuBar );
}

iaMainFrame::~iaMainFrame()
{

}

wxAuiManager *iaMainFrame::GetAuiManager() const
{
    return auiManager;
}

bool iaMainFrame::CloseIDE()
{
    if( m_editorManager->CloseAllEditors() )
    {
        const std::vector<sdk::pluginData> &vectorPlugins = sdk::PluginManager::Get()->GetPlugins();
        for( size_t i=0; i<vectorPlugins.size(); ++i )
            if( !vectorPlugins[i].plugin->OnClose() )
                return false;

        SaveIDE();

        return true;
    }

    return false;
}

void iaMainFrame::SaveIDE()
{
    sdk::FileConfig *fcLayout = configManager->GetFile( sdk::Path::s_dirConfig + wxT( "settings_layout.xml" ) );

    fcLayout->WriteString( wxT( "/perspective/manager/editor" ),
        auiManager->SavePaneInfo( auiManager->GetPane( m_editorManager->GetNotebookEditor() ) ) );
    fcLayout->WriteString( wxT( "/perspective/manager/log" ),
        auiManager->SavePaneInfo( auiManager->GetPane( logManager ) ) );
    fcLayout->WriteString( wxT( "/perspective/manager/project" ),
        auiManager->SavePaneInfo( auiManager->GetPane( projectManager ) ) );
    fcLayout->WriteString( wxT( "/perspective/toolbar/main" ),
        auiManager->SavePaneInfo( auiManager->GetPane( mainToolBar ) ) );
    fcLayout->WriteString( wxT( "/perspective/manager" ),
        auiManager->SavePerspective() );

    fcLayout->Save();
}



void iaMainFrame::OnClose( wxCloseEvent &evt )
{
    if( !CloseIDE() )
        evt.Veto();
    else
        evt.Skip();
}

void iaMainFrame::OnUpdateStyledEditor( wxScintillaEvent &evt )
{
    sdk::Editor *se = dynamic_cast<sdk::Editor *>( evt.GetEventObject() );

    sbar->SetStatusText( wxString::Format( wxT( "Line %d, Column %d" ), se->GetCurrentLine()+1, se->GetColumn( se->GetCurrentPos() )+1 ), 2 );
    sbar->SetStatusText( ( se->GetOvertype() ? wxT( "Overwrite" ) : wxT( "Insert" ) ), 3 );
    sbar->SetStatusText( ( se->GetModify() ? wxT( "Modified" ) : wxT( "" ) ), 4 );
    sbar->SetStatusText( ( se->GetReadOnly() ? wxT( "Read" ) : wxT( "Read/Write" ) ), 5 );
}

void iaMainFrame::OnPageChanged( wxAuiNotebookEvent &evt )
{
    sdk::Editor *se = m_editorManager->GetCurrentEditor();
    if( se != NULL )
    {
        sbar->SetStatusText( se->GetFileName(), 0 );
        sbar->SetStatusText( se->GetFontEncoding(), 1 );
        sbar->SetStatusText( wxString::Format( wxT( "Line %d, Column %d" ), se->GetCurrentLine()+1, se->GetColumn( se->GetCurrentPos() )+1 ), 2 );
        sbar->SetStatusText( ( se->GetOvertype() ? wxT( "Overwrite" ) : wxT( "Insert" ) ), 3 );
        sbar->SetStatusText( ( se->GetModify() ? wxT( "Modified" ) : wxT( "" ) ), 4 );
        sbar->SetStatusText( ( se->GetReadOnly() ? wxT( "Read" ) : wxT( "Read/Write" ) ), 5 );
    }
}


///FILE MENU
void iaMainFrame::OnNewEmptyFile( wxCommandEvent &evt )
{
    m_editorManager->NewFile();
    evt.Skip();
}

void iaMainFrame::OnOpenFile( wxCommandEvent &evt )
{
    wxFileDialog dlg( this, wxT( "Open file" ), wxT( "" ), wxT( "" ), wxT( "All files(*)|*" ) );

    int res = dlg.ShowModal();

    if( res == wxID_OK )
        m_editorManager->OpenFile( dlg.GetPath() );

    evt.Skip();
}

void iaMainFrame::OnSaveFile( wxCommandEvent &evt )
{
    m_editorManager->SaveCurrentEditor();
    evt.Skip();
}

void iaMainFrame::OnSaveFileAs( wxCommandEvent &evt )
{
    m_editorManager->SaveEditorAs();
    evt.Skip();
}

void iaMainFrame::OnSaveAllFiles( wxCommandEvent &evt )
{
    m_editorManager->SaveAllEditors();
    evt.Skip();
}

void iaMainFrame::OnCloseFile( wxCommandEvent &evt )
{
    m_editorManager->CloseCurrentEditor();
    evt.Skip();
}

void iaMainFrame::OnCloseAllFiles( wxCommandEvent &evt )
{
    m_editorManager->CloseAllEditors();
    evt.Skip();
}

void iaMainFrame::OnExit( wxCommandEvent &evt )
{
    if( CloseIDE() )
        Close();

    evt.Skip();
}

void iaMainFrame::OnUpdateFileMenu( wxUpdateUIEvent &evt )
{
    sdk::Editor *se = m_editorManager->GetCurrentEditor();

    bool canSave( false );
    bool canSaveAs( false );
    bool canSaveAll( false );
    bool canClose( false );
    bool canCloseAll( false );

    if( se )
    {
        canSave = ( se->GetModify() );
        canSaveAs = true;
        canSaveAll = true;
        canClose = true;
        canCloseAll = true;
    }

    wxMenuBar *mbar = GetMenuBar();

    mbar->Enable( iaID_FILE_SAVE_FILE, canSave );
    mbar->Enable( iaID_FILE_SAVE_FILE_AS, canSaveAs );
    mbar->Enable( iaID_FILE_SAVE_ALL_FILES, canSaveAll );
    mbar->Enable( iaID_FILE_CLOSE_FILE, canClose );
    mbar->Enable( iaID_FILE_CLOSE_ALL_FILES, canCloseAll );

    if( auiManager->GetPane( mainToolBar ).IsShown() )
    {
        mainToolBar->EnableTool( iaID_FILE_SAVE_FILE, canSave );
        mainToolBar->EnableTool( iaID_FILE_SAVE_ALL_FILES, canSaveAll );
    }

    evt.Skip();
}



///EDIT MENU
void iaMainFrame::OnEditMenu( wxCommandEvent &evt )
{
    sdk::Editor *se = m_editorManager->GetCurrentEditor();

    switch( evt.GetId() )
    {
        case iaID_EDIT_UNDO:
            se->Undo();
        break;

        case iaID_EDIT_REDO:
            se->Redo();
        break;

//        case iaID_EDIT_CLEAR_HISTORY:
//            se->EmptyUndoBuffer();
//        break;

        case iaID_EDIT_CUT:
            se->Cut();
        break;

        case iaID_EDIT_COPY:
            se->Copy();
        break;

        case iaID_EDIT_PASTE:
            se->Paste();
        break;

        case iaID_EDIT_SELECT_ALL:
            se->SelectAll();
        break;

        case iaID_EDIT_AUTOCOMPLETE:
            se->Autocomplete( true );
        break;
    }
}

void iaMainFrame::OnUpdateEditMenu( wxUpdateUIEvent &evt )
{
    const sdk::Editor *se = m_editorManager->GetCurrentEditor();

    bool canUndo( false );
    bool canRedo( false );
    //bool canEmptyUndoBuffer( false );
    bool canCut( false);
    bool canCopy( false );
    bool canPaste( false );
    bool canSelectAll( false );
    bool canAutocomplete( false );

    if( se )
    {
        canUndo = se->CanUndo();
        canRedo = se->CanRedo();
        //canEmptyUndoBuffer = canUndo || canRedo;
        canCut = se->CanCut();
        canCopy = se->CanCopy();
        canPaste = se->CanPaste();
        canSelectAll = true;
        canAutocomplete = true;
    }

    wxMenuBar *mbar = GetMenuBar();

    mbar->Enable( iaID_EDIT_UNDO, canUndo );
    mbar->Enable( iaID_EDIT_REDO, canRedo );
    //mbar->Enable( iaID_EDIT_CLEAR_HISTORY, canEmptyUndoBuffer );
    mbar->Enable( iaID_EDIT_CUT, canCut );
    mbar->Enable( iaID_EDIT_COPY, canCopy );
    mbar->Enable( iaID_EDIT_PASTE, canPaste );
    mbar->Enable( iaID_EDIT_SELECT_ALL, canSelectAll );
    mbar->Enable( iaID_EDIT_AUTOCOMPLETE, canAutocomplete );

    if( auiManager->GetPane( mainToolBar ).IsShown() )
    {
        mainToolBar->EnableTool( iaID_EDIT_UNDO, canUndo );
        mainToolBar->EnableTool( iaID_EDIT_REDO, canRedo );
        mainToolBar->EnableTool( iaID_EDIT_CUT, canCut );
        mainToolBar->EnableTool( iaID_EDIT_COPY, canCopy );
        mainToolBar->EnableTool( iaID_EDIT_PASTE, canPaste );
    }

    evt.Skip();

}



///VIEW MENU
void iaMainFrame::OnViewMenu( wxCommandEvent &evt )
{
    switch( evt.GetId() )
    {
        case iaID_VIEW_TOOLBAR_MAIN:
            auiManager->GetPane( mainToolBar ).Show( evt.IsChecked() );
        break;

        case iaID_VIEW_PROJECT:
            auiManager->GetPane( projectManager ).Show( evt.IsChecked() );
        break;

        case iaID_VIEW_LOG:
            auiManager->GetPane( logManager ).Show( evt.IsChecked() );
        break;

        case iaID_VIEW_STATUS_BAR:
            GetStatusBar()->Show( evt.IsChecked() );
        break;
    }

    auiManager->Update();
}

void iaMainFrame::OnUpdateViewMenu( wxUpdateUIEvent &evt )
{
    bool checkProject = auiManager->GetPane( projectManager ).IsShown();
    bool checkLog = auiManager->GetPane( logManager ).IsShown();
    bool checkStatusBar = GetStatusBar()->IsShown();
    bool checkToolBarMain = auiManager->GetPane( mainToolBar ).IsShown();

    wxMenuBar *mbar = GetMenuBar();

    mbar->Check( iaID_VIEW_PROJECT, checkProject );
    mbar->Check( iaID_VIEW_LOG, checkLog );
    mbar->Check( iaID_VIEW_STATUS_BAR, checkStatusBar );
    mbar->Check( iaID_VIEW_TOOLBAR_MAIN, checkToolBarMain );

    evt.Skip();
}




/// SEARCH MENU
void iaMainFrame::OnFind( wxCommandEvent &evt )
{
    iaManager::Get()->GetDialogManager()->GetFindDialog()->ShowModal();
    evt.Skip();
}

void iaMainFrame::OnFindNext( wxCommandEvent &evt )
{
    iaManager::Get()->GetDialogManager()->GetFindDialog()->Find( false );
    evt.Skip();
}

void iaMainFrame::OnFindPrevious( wxCommandEvent &evt )
{
    iaManager::Get()->GetDialogManager()->GetFindDialog()->Find( true );
    evt.Skip();
}

void iaMainFrame::OnReplace( wxCommandEvent &evt )
{
    iaManager::Get()->GetDialogManager()->GetReplaceDialog()->ShowModal();
    evt.Skip();
}

void iaMainFrame::OnGotoLine( wxCommandEvent &evt )
{
    sdk::Editor *se = m_editorManager->GetCurrentEditor();

    long numberLine;
    long lineCount( se->GetLineCount() );
    if( wxGetTextFromUser( wxString::Format( wxT( "Line (1 - %ld)" ), lineCount ), wxT( "Goto line..." ) ).ToLong( &numberLine ) )
    {
        if( numberLine <= lineCount && numberLine > 0 )
            se->GotoLine( numberLine-1 );
    }
}

void iaMainFrame::OnUpdateSearchMenu( wxUpdateUIEvent &evt )
{
    sdk::Editor *se = m_editorManager->GetCurrentEditor() ;

    bool canFind( false );
    bool canReplace( false );

    if( se )
    {
        canFind = true;
        canReplace = !se->GetReadOnly();
    }

    wxMenuBar *mbar = GetMenuBar();

    mbar->Enable( iaID_SEARCH_FIND, canFind );
    mbar->Enable( iaID_SEARCH_FIND_NEXT, canFind );
    mbar->Enable( iaID_SEARCH_FIND_PREV, canFind );
    mbar->Enable( iaID_SEARCH_REPLACE, canReplace );
    mbar->Enable( iaID_SEARCH_GOTO_LINE, canFind );

    if( auiManager->GetPane( mainToolBar ).IsShown() )
    {
        mainToolBar->EnableTool( iaID_SEARCH_FIND, canFind );
        mainToolBar->EnableTool( iaID_SEARCH_REPLACE, canReplace );
    }

    evt.Skip();
}



///SETTINGS
void iaMainFrame::OnSettingsEditor( wxCommandEvent &evt )
{
    iaSettingsEditorDialog dlg( this );
    dlg.ShowModal();
}

void iaMainFrame::OnSettingsPlugins( wxCommandEvent &evt )
{
    sdk::PluginsManagerDialog dlg( this );
    dlg.ShowModal();
}


///ABOUT
void iaMainFrame::OnHelpAbout( wxCommandEvent &evt )
{
    iaAboutDialog dlg( this );
    dlg.ShowModal();
}
